<?php require ("header.php"); ?>
<?php 
	//check admin privillege before displaying the page
	if ($ad_check<1){
			header ("Location:../login.php?action=0");
	} 
	$limit=20;
	if (isset($_GET['id'])){
		$data=$_GET['id'];
	} else {
		$data=0;
	}
	//Setting the sorting thingy here.
	if (isset($_POST['sort']))
	{
		$course=$_POST['course']; 	//dapatkan data dari borang sorting
		$sem=$_POST['sem'];			//dapatkan data dari borang sorting
		$class=$_POST['class'];		//dapatkan data dari borang sorting
		if ($course != NULL && $sem != NULL && $class != NULL)
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course' AND stud_sem='$sem' AND class='$class' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course' AND stud_sem='$sem' AND class='$class'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}
		else if ( $course == NULL && $sem != NULL  && $class != NULL ) 
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_sem='$sem' AND class='$class' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_sem='$sem' AND class='$class'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}
		else if ( $course == NULL && $sem == NULL && $class != NULL )
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND class='$class' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND class='$class'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}
		else if ( $course != NULL && $sem == NULL && $class==NULL )
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}
		else if ( $course != NULL && $sem != NULL && $class == NULL )
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course' AND stud_sem='$sem' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_course='$course' AND stud_sem='$sem'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}
		else if ( $course==NULL && $sem != NULL && $class==NULL)
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_sem='$sem' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep' AND stud_sem='$sem'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
	 	}
		else 
		{
			$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
			$ad_dep=mysql_query($sql_ad_dep);
			$admin_dep=mysql_fetch_assoc($ad_dep);
			$dep=$admin_dep['ad_department'];
			$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' 
							ORDER BY stud_id ASC LIMIT $data,$limit";
			$get_data=mysql_query($sql_get_data);
			//get total of data from database.
			$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep'";
			$get_row=mysql_query($sql_get_row);
			$num_row=mysql_num_rows($get_row);
		}	
	} else {
		//get admin department to select only equivalent student data being display according to department of the admin
		$sql_ad_dep="SELECT ad_department FROM admin WHERE ad_no='$id'";
		$ad_dep=mysql_query($sql_ad_dep);
		$admin_dep=mysql_fetch_assoc($ad_dep);
		$dep=$admin_dep['ad_department'];
		$sql_get_data="SELECT * FROM student WHERE stud_deparment='$dep' ORDER BY stud_id ASC LIMIT $data,$limit";
		$get_data=mysql_query($sql_get_data);
		//get total of data from database.
		$sql_get_row="SELECT * FROM student WHERE stud_deparment='$dep'";
		$get_row=mysql_query($sql_get_row);
		$num_row=mysql_num_rows($get_row);
	}
	//get total page
	$total_page=intval($num_row/$limit);
	$temp = $num_row-($total_page*$limit);
	if ($temp > 0 ) { 
		$total_page++;
	}
	if ($data>$num_row) {
		echo "cannot process your request.  Your request exceed the amount of data in database.";
		exit();
	}
	
	//get current page
	$current=intval(($data+$limit)/$limit);
	
	//set the previous and next link to be valid or not.
	if ($data<1) {
		$previous=0; // previous link is invalid
	} else {
		$previous=1; // previous link is valid
	}
	if (($num_row-$data)<=$limit) {
		$next=0;  // next link is invalid
	} else {
		$next=1;  // next link is valid
	}
	
?>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
.style1 {color: #000000}
.style3 {color: #000000; font-weight: bold; }
-->

</style>
<?php // Begin displaying data after this line ?>
<form method="POST" action="student_stat.php">
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div align="right" class="style1">Sort By Course :
        <select name="course">
		<option></option>
		<?php 
			$sql_get_course="SELECT * FROM course WHERE course_dep='$dep'";
			$get_course=mysql_query($sql_get_course);
			while ($c=mysql_fetch_assoc($get_course))
			{ ?>
				<option value="<?php echo $c['course_code']; ?>"><?php echo $c['course_code']; ?></option> <?php
			} ?>
      </select>
        Semester: 
        <select name="sem" id="sem">
		<option></option>
		<?php
			$sql_get_sem="SELECT * FROM semester";
			$get_sem=mysql_query($sql_get_sem);
			while ($s=mysql_fetch_assoc($get_sem))
			{ ?>
				<option value="<?php echo $s['sem_num']; ?>"><?php echo $s['sem_num']; ?></option>
			<?php } ?>
        </select> 
        Class: 
        <select name="class" id="class">
	      <option></option>	
          <option value="A">A</option>
          <option value="B">B</option>
          <option value="C">C</option>
        </select>
        <input name="sort" type="submit" id="sort" value="Sort" />
    </div></td>
  </tr>
</table>
 </form>
<table width="780" border="0" align="center">
  <tr>
    <td width="193"><span class="style1">List result <?php echo $data+1; ?> - 
      <?php 
		if (($num_row < $limit) || (($num_row-$data)<$limit)) { 
			echo $num_row; 
		} else { 
			echo ($data+$limit); 
		} 
	?> 
    </span></td>
    <td width="188"><span class="style1"></span></td>
    <td width="185"><span class="style1"></span></td>
    <td width="196"><div align="right" class="style1">
		<?php if ($previous==1) {
			echo "<a href=\"page.php?id=" .($data-$limit)."\">Previous</a>";
		} else { ?>Previous<?php } ?>
		 | Page <?php echo $current; ?> | 
		<?php if ($next==1) {
			echo "<a href=\"page.php?id=" .($data+$limit)."\">Next</a>";
		} else { ?>Next<?php } ?> </div></td>
  </tr>
</table>
<br>
<table width="780" border="1" align="center" bordercolor="#000000" bgcolor="#000000">
  <tr>
    <td width="19" bgcolor="#CCCCCC"><div align="center"></div></td>
    <td width="258" bgcolor="#CCCCCC"><div align="center"><span class="style3">Student Name </span></div></td>
    <td width="101" bgcolor="#CCCCCC"><div align="center"><span class="style3">Student ID </span></div></td>
    <td width="72" bgcolor="#CCCCCC"><div align="center"><span class="style3">Course</span></div></td>
    <td width="37" bgcolor="#CCCCCC"><div align="center"><span class="style3">Sem</span></div></td>
    <td width="115" bgcolor="#CCCCCC"><div align="center"><span class="style3">Department</span></div></td>
    <td bgcolor="#CCCCCC"><div align="center" class="style1"><strong>Action</strong></div></td>
  </tr>
  <tr>
  	<?php 
	$count=$data;
	while ($value=mysql_fetch_assoc($get_data)) { ?>
    <td bgcolor="#FFFFFF" valign="top"><div align="center"><span class="style1"><?php echo $count+1; ?></span></div></td>
    <td bgcolor="#FFFFFF" valign="top"><div align="left"><span class="style1"><?php echo $value['stud_name']; ?></span></div></td>
    <td bgcolor="#FFFFFF" valign="top"><div align="center"><span class="style1"><?php echo $value['stud_no']; ?></span></div></td>
    <td bgcolor="#FFFFFF" valign="top"><div align="center"><span class="style1"><?php echo $value['stud_course']; ?></span></div></td>
    <td bgcolor="#FFFFFF" valign="top"><div align="center"><span class="style1"><?php echo $value['stud_sem']; ?></span></div></td>
    <td bgcolor="#FFFFFF" valign="top"><div align="center"><span class="style1"><?php echo $value['stud_deparment']; ?></span></div></td>
    <td valign="top" bgcolor="#FFFFFF"><div align="center"><a href ="<?php echo "stat_detail.php?id={$value['stud_no']}";?>">See Student Mark</a> </div></td>
  </tr>
  <?php $count++; } ?>
</table>
<br>
<table width="780" border="0" align="center">
  <tr>
    <td width="194"><span class="style1">List result <?php echo $data+1; ?> - 
      <?php 
		if (($num_row < $limit) || (($num_row-$data)<$limit)) { 
			echo $num_row; 
		} else { 
			echo ($data+$limit); 
		} 
	?> 
    </span></td>
    <td width="187"><span class="style1"></span></td>
    <td width="185"><span class="style1"></span></td>
    <td width="196"><div align="right" class="style1">
		<?php if ($previous==1) {
			echo "<a href=\"page.php?id=" .($data-$limit)."\">Previous</a>";
		} else { ?>Previous<?php } ?>
		 | Page <?php echo $current; ?> | 
		<?php if ($next==1) {
			echo "<a href=\"page.php?id=" .($data+$limit)."\">Next</a>";
		} else { ?>Next<?php } ?> </div></td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>

<?php require ("footer.php"); ?>